# ============================================================================
# Service Common Library
# ============================================================================
# This library contains shared protobuf definitions and common code
# used by both server and client applications.
# ============================================================================

# Define proto file paths
set(PB_SRC ${CMAKE_CURRENT_SOURCE_DIR}/proto/helloworld.proto)
message(STATUS "service_common proto files: ${PB_SRC}")

# Create output directory for generated files
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src)
# ============================================================================
# Generate Protobuf Code
# ============================================================================
# Generate standard protobuf code (.pb.cc/.pb.h)
# Output to src directory without preserving proto/ subdirectory structure
# Parameters:
#   - OUT_PB_SRCS: Output variable for generated source files
#   - PB_SRC: Input proto files
#   - PB_PROTOC: Protobuf compiler path
#   - CMAKESOURCEDIR: Proto file directory (for -I and WORKING_DIRECTORY)
#   - PBTARGETDIR: Output directory
COMPILE_PROTO_TO_TARGETDIR(
    OUT_PB_SRCS 
    "${PB_SRC}" 
    ${PB_PROTOC} 
    ${CMAKE_CURRENT_SOURCE_DIR}/proto 
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Generate tRPC code (.trpc.pb.cc/.trpc.pb.h)
# Output to src directory without preserving proto/ subdirectory structure
TRPC_COMPILE_PROTO_TO_TARGETDIR(
    OUT_TRPC_PB_SRCS 
    "${PB_SRC}" 
    ${PB_PROTOC} 
    ${TRPC_CPP_PLUGIN} 
    ${CMAKE_CURRENT_SOURCE_DIR}/proto 
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)
# ============================================================================
# Create Static Library
# ============================================================================
add_library(service_common STATIC 
    ${OUT_PB_SRCS}
    ${OUT_TRPC_PB_SRCS}
)

# Add include directories (generated headers are in src directory)
target_include_directories(service_common 
    PUBLIC 
        ${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Link dependencies
target_link_libraries(service_common 
    PUBLIC
        ${LIBRARY}
)

# Set library properties
set_target_properties(service_common PROPERTIES
    POSITION_INDEPENDENT_CODE ON
    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
)
